define(["utils", "./leftTreeHeaderItem", "./leftTreePersonItem"], function (utils, HeaderItemClass, PersonItemClass) {

    var Component = function (config) {
        var _this = this;
        this.selector = config.selector;
        this.itemData = config.itemData;
        this.personItems = [];
        this.deptId = this.itemData.value.deptId.toString();
        this.selector.leftGroups[this.deptId] = this;
        this.group = config.group;
        this.tree = config.tree;
        this.groups = [];
        this.hasLoadPersonData = false;
        this.$el = $("<div class='person-selector-group'></div>");
        this.headerItem = new HeaderItemClass({
            selector: this.selector,
            itemData: this.itemData,
            group: this
        });
        this.groupWrap = $("<div style='padding-left:22px;height:0px' class='person-selector-groupwrap'></div>");
        this.init(this.selector.DeptDictData[this.deptId]);
        this.$el.append(this.headerItem.$el).append(this.groupWrap);
    }

    Component.prototype = {
        personItemToggle: function (personItemInstance) {
            var itemStatus = personItemInstance.status;
            if (itemStatus === "checked") {
                personItemInstance.unChecked();
                for (var i = 0; i < this.selector.hasSelected.length; i++) {
                    if (this.selector.hasSelected[i].value.accountId == personItemInstance.itemData.value.accountId) {
                        this.selector.hasSelected.splice(i, 1);
                    }
                }
            } else {
                personItemInstance.checked();

                this.selector.hasSelected.push(personItemInstance.itemData);
            }
            this.parentGroupCheckedIfNeed();
        },

        _parentGroupCheckedIfNeed: function () {
            var childLength = this.personItems.length + this.groups.length;
            var checkedLength = 0;
            for (var i = 0, j = this.personItems.length; i < j; i++) {
                if (this.personItems[i].status === "checked") {
                    checkedLength += 1;
                }
            }
            for (var n = 0, m = this.groups.length; n < m; n++) {
                var group = this.groups[n];
                if (group.headerItem.status === "checked") {
                    checkedLength += 1;
                } else if (group.headerItem.status === "indeterminate") {
                    checkedLength += 0.5;
                }
            }
            if (checkedLength === 0 && childLength !== 0) {
                this.headerItem.setCheck("unChecked");
            }else if(childLength===0){
                if(this.headerItem.status==="unChecked"){
                    this.headerItem.setCheck("unChecked");
                }else{
                    // this.headerItem.setCheck("checked");
                }
            }  else if (checkedLength === childLength) {
                // this.headerItem.setCheck("checked");
            } else {
                this.headerItem.setCheck("indeterminate");
            }
        },
        parentGroupCheckedIfNeed: function () {
            var _group = this;
            _group._parentGroupCheckedIfNeed();
            while (_group.group) {
                _group.group._parentGroupCheckedIfNeed();
                _group = _group.group;
            }
        },
        setParentGroupIndeterminate: function (deptId) {
            var _group = this;
            // _group.headerItem.setCheck("indeterminate");
            while (_group.group) {
                _group.group.headerItem.setCheck("indeterminate");
                _group = _group.group;
            }
        },
        getResult:function(){
          // 如果左边的headerItem 选中的话  那么就取整个部门
          // 如果左边的headerItem没有选中 则不向下遍历了
          // 如果左边的headerItem半选的话  那么就向下遍历 获取部门和人员
          var GroupStatus = this.headerItem.status;
          if(GroupStatus==="checked"){
            this.selector.leftTree.deptResult.push(this.itemData.value);
          }else if(GroupStatus==="unChecked"||GroupStatus==="indeterminate"){
            for(var n=0,m=this.groups.length;n<m;n++){
              this.groups[n].getResult();
            }
          }


        },
        groupHeaderItemCheckChange: function () {
            var status = this.headerItem.status;

            if (status === "checked") {
                this.clearallCheckChildren();

                // // 从缓存数据中删除部门
                // for (var i = 0; i < this.selector.hasSelected.length; i++) {
                //     if (this.selector.hasSelected[i].value.deptId == this.itemData.value.deptId) {
                //         this.selector.hasSelected.splice(i, 1);
                //     }
                // }
            } else {
                this.checkAllChildren();

                // 从缓存数据中添加部门
                //this.selector.hasSelected.push(this.itemData);

            }
            this.parentGroupCheckedIfNeed();
        },
        checkAllChildren: function () {

            this.closeAllChildGroup();
            this.headerItem.setCheck("checked");

            for (var i = 0, j = this.groups.length; i < j; i++) {
                this.groups[i].checkAllChildren();
            }
            for (var n = 0, m = this.personItems.length; n < m; n++) {
                this.personItems[n].checked();
            }
        },
        clearallCheckChildren: function () {
            this.headerItem.setCheck("unChecked");

            for (var i = 0, j = this.groups.length; i < j; i++) {
                this.groups[i].clearallCheckChildren();
            }
            for (var n = 0, m = this.personItems.length; n < m; n++) {
                this.personItems[n].unChecked();
            }
        },
        fillPerson: function (data) {
            this.hasLoadPersonData = true;
            if (this.personItems.length > 0) {
                return;
            }
            for (var i = 0, j = data.length; i < j; i++) {
                var personItem = new PersonItemClass({
                    selector: this.selector,
                    itemData: data[i],
                    group: this
                });
                this.personItems.push(personItem);
                if (this.headerItem.status === "checked") {
                    personItem.checked();
                }
                this.groupWrap.append(personItem.$el);
            }
        },
        toggle: function () {
            if (this.isOpen) {
                this.close()
            } else {
                this.open();
            }
        },
        close: function () {

            this.isOpen = false;
            this.headerItem.triangle.removeClass("person-selector-itemopen");
            this.groupWrap.css({height: "0px"});
        },

        open: function () {

            this.isOpen = true;
            this.groupWrap.css({height: "auto"});

            this.headerItem.triangle.addClass("person-selector-itemopen");
        },
        closeAllChildGroup: function () {
            var group = this;
            group.close();
            for (var i = 0, j = this.groups.length; i < j; i++) {
                this.groups[i].close();
            }
        },
        openAllChildGroup: function () {
            var group = this;
            group.open();
            for (var i = 0, j = this.groups.length; i < j; i++) {
                this.groups[i].open();
            }
        },

        init: function (data) {
            //部门都是group
            if (!data) {
                return;
            }
            for (var i = 0, j = data.length; i < j; i++) {
                var group = new Component({
                    selector: this.selector,
                    tree: this.tree,
                    group: this,
                    isRoot: false,
                    itemData: data[i]
                });
                this.groups.push(group);
                this.groupWrap.append(group.$el);
            }
        }
    };


    return Component;
});
